import pageination from '../../components/pageination/pageination.vue'
import urls from "../../common/js/api_url_array";
export default {
	data() {
		return {
			//数据
      modelShowView: false, // 合同查看

      logId: 0,
      logPage: 1,
      logTotal: 0,


      type: 0,
      obj: {},
      contractBeginTime: '',
      contractEndTime: '',
      phoneNumber: '',
      contract: '', // 合同附件
      projectId: '', // 上传合同的项目id
      expert: {},
      projectObj: {},
      project: [], // 项目
      logData: [], // log
      baseId: [],
      thatUrl: this.urls.baseUrl,
      experts: [], // 专家table
			modelShow: false,
			ProjectModelShow: false,
			demandModelShow: false,
			WorkModelShow: false,
			total: 0, //总条数
			page: 1, //当前页
			size: 10, //每页显示条数
			uploadNum: 0,
			FileUpload: [],
			 curId: 1,
      userId: this.get("userInfo").id,
      curIndex:-1,//选中下标
      validate: {}
		}
	},
	components: {
		pageination
	},
	created() { //调用方法
		let that = this;
		that.getBase();
	},
	mounted() {
    jQuery.validator.addMethod("positiveinteger", function(value, element) {
      var aint = parseInt(value);
      return aint>0&& (aint+"")==value;
    }, "Please enter a valid number.");

    //加载表单验证
    this.validate = $("#contractForm").validate({
      ignore: [],
      rules: {
        contractBeginTime: { // 名称
          required: true,
        },
        contractEndTime: { // 身份证
          required: true,
        },
        guide: { // 邮箱
          required: true,
          positiveinteger:true
        },


      },
      messages: {
        contractBeginTime: { // 名称
          required: "请选择开始时间!",
        },
        contractEndTime: { // userAccount
          required: "请选择结束时间!",
        },
        guide: { // userAccount
          required: "请输入指导次数!",
          positiveinteger:  '只能输入正整数!',
        },
      },
      /*errorElement: "em",
            errorPlacement: function(error, element) {
                error.appendTo(element.parents(".error-box").children(".error-msg"));
            }*/
    });

  },
	methods: { //写函数方法
		//给选中的列加背景色
		choosed:function(index){
			this.curIndex=index;
		},
    closeWorkModel: function () {
      this.WorkModelShow = false;
    },

    pageFnExpertsLog: function(val, pid){
      this.logPage = parseInt(val);
      this.rorkRecord();
    },  //专家log

    /**返回子组件用户选择的一页显示条数**/
    obtainChildExpertsLog: function(data) {
      this.row = data;
    },

    //对接专家团队
    getExperts:function(){
      // 專家信息
      let that = this;
      console.log(that.baseId)
      if(that.baseId.length < 1){
        return;
      };
      let name = $('#searchRoleVal').val();
      that.experts = [];
     let obj =   {
        "list": that.baseId, "pageNum": that.page, "pageSize": 10
      };
      obj[$('#searchSelect').val()] = name;

      that.$post(
        that.urls.getBaseExperts,
        obj
      )
        .then(function(res) {
          if(res.success) {
            if(res.success){
              that.experts = res.pageInfo.list;
              that.page =  res.pageInfo.pageNum;
              that.total = res.pageInfo.total;
            }
          }
        })
    },

    // 基地数据
    getBase: function ( ) {
      let that = this;
      if(! that.userId){return;};
      // 基地展示数据
      that.$post(
        that.urls.basePage,
        { "pageNum": 0, "list": [1,2,5],
          "pageSize": 9999999 , "userId": that.userId}
      )
        .then(function(res) {
          if(res.success) {
            let select = [];
            res.pageInfo.list.forEach(function (e) {
              select.push( e.id );
            })
            that.baseId = select;
            that.getExperts();
          }
        })
    },

    // 搜索
    searchExpert: function () {
      this.getExperts();
    },

		//页码切换执行方法
		pageFn: function(val, pid) {
			this.page = parseInt(val);
			this.getExperts();
			//此处调列表数据接口
		},
		/**返回子组件用户选择的一页显示条数**/
		obtainChild: function(data) {
			this.row = data;
		},





		touchOffIdInput(n) {
			this.uploadNum = n;
			if(n == 1) {
				$("#handIdFront").click();
			}
		},
    /** 合同附件文件上传(多文件)js**/
    idFrontImg: function(event) {
      let that = this;
      let files = event.currentTarget.files;
      if (!that.fileGlobalFilter(files)) {
        return;
      }
      let formData = new FormData();
      for(let i = 0, len = files.length; i < len; i++) {
        formData.append('fileNames', files[i]);
      }
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
        }
      };
      that.$post(
        that.urls.upload,
        formData,
        config
      )
        .then(function(res) {
          if(res.success) {
            let fileInfos = res.data;
            let urls = [];
        //    let names = [];
            let enclosureList = [];
            for(let i = 0, len = fileInfos.length; i < len; i++) {
              urls.push(fileInfos[i].url);
            //  names.push(that.getFileName(fileInfos[i].url));
              enclosureList.push({
                "name": that.getFileName(fileInfos[i].url),
                "url":  fileInfos[i].url
              });
            }
            that.FileUpload = enclosureList;
            that.contract = urls.join(";");
          }
        })
      console.log(that.FileUpload )
    },
		//打开合同信息模态框
		openModal: function(id, o, type) {
      let that = this;
      that.validate.resetForm();
      if(type){
        that.type = type;
      }
      that.obj = {};
      that.contract = '';
      that.FileUpload = [];
      that.contractBeginTime = '';
      that.contractEndTime = '';
      that.modelShow = true;
      that.projectId = id;

      $('#guide').val(o.guide);
      if(o.guide) {
      //  that.obj = o;
        that.contract = o.contract;
        that.contractBeginTime = o.contract_begin_time;
        that.contractEndTime = o.contract_end_time;

		console.log(o)
        let enclosureList = [];  // 查看
        if(o.contract){
	        enclosureList.push({
	          "name": that.getFileName(that.getFileName(o.contract)),
	        });
        }
       
        that.FileUpload = enclosureList;
      }

		},

    changeInput: function () {
      this.validate.resetForm();
   /*   let that = this;
      let flg = that.validate.form();
      if(! flg){
        return;
      }*/
    },

    // 合同提交
    subcontractForm: function () {
		  let that = this;
		  let obj = combinationFormObject($('#contractForm').serializeArray());
      obj.id = that.projectId;
       if(! obj.id){ return;};

      let flg = that.validate.form();
      if(! flg){
        return;
      }

      let starttime = new Date(obj.contractBeginTime);
      let endtime = new Date(obj.contractEndTime);
   //   obj.contractBeginTime = that.contractBeginTime;
   //   obj.contractEndTime = that.contractEndTime;
      //进行比较
      console.log(obj)


      if(endtime <= starttime){
        layer.msg('结束时间不能小于或等于开始时间!', {
          time: 3000, //3s后自动关闭
        });
        return;
      }
      that.$post(
        that.urls.updateProjectInfo,
              obj
      )
        .then(function(res) {
          if(res.success) {
            layer.msg('成功保存合同！', {
              time: 3000, //3s后自动关闭
            });
            that.modelShow = false;
            that.getExperts();
          }else{
            layer.msg('保存合同失败！', {
              time: 3000, //3s后自动关闭
            });
          }
        })
		  console.log(obj)
    },

   // 合同查看
    modelShowViewThe : function (o ) {
      let that = this;
      that.obj = o;
      that.modelShowView = true;
    //  that.contract = o.contract;
      that.contractBeginTime = o.contract_begin_time;
      that.contractEndTime = o.contract_end_time;
    },

    // 项目具体情况
    viewProject: function (obj) {
      let that = this;
      that.projectObj ={};
      that.curId = 1;
      that.ProjectModelShow = true;
      that.projectObj = obj;
    },

		//通知提醒模态框
		demand: function(num) {
      if(! num){
        layer.msg('手机号码为空！', {
          time: 3000, //3s后自动关闭
        });
      }
      this.phoneNumber = num;
			this.demandModelShow = true;
		},

    // 通知 提交
    subDemand: function () {
      let that = this;

      if(!$('#smscontent').val()){    layer.msg('内容不能为空!', {
        time: 3000, //3s后自动关闭

      });  return;  };

      if($('#smscontent').val().length > 100 ){    layer.msg('内容最多只能输入100字!', {
        time: 3000, //3s后自动关闭

      });  return;  };


      that.$post(
        that.urls.sendMsg,
        {"receiver": that.phoneNumber, "smscontent": $('#smscontent').val()}
      )
        .then(function(res) {
          if(res.success) {
            layer.msg('消息提醒成功!', {
              time: 3000, //3s后自动关闭
            });
            that.demandModelShow = false;
          }else{
            layer.msg('消息提醒失败!', {
              time: 3000, //3s后自动关闭
            });
          }
        })

    },
		//工作记录
		rorkRecord: function(id) {
      let that = this;
      that.logData = [];
       console.log(id)
      if(id){
        that.logPage = 1;
        that.logId = id;
      }

      that.$post(
        that.urls.getLogPage,
        {"pageNum": that.logPage,
          "pageSize": 10, "projectId": that.logId}
      )
        .then(function(res) {
          if(res.success) {
            that.logData = res.pageInfo.list;
            that.logPage = res.pageInfo.pageNum;
            that.logTotal = res.pageInfo.total;
          }
        })
			this.WorkModelShow = true;
		},
		//关闭模态框
		closeModel: function() {
  this.modelShowView = false;
			this.modelShow = false;
			this.demandModelShow = false;
			this.WorkModelShow = false;
		},
    closeModels: function () {
      this.ProjectModelShow = false;
    },

    projectDetails: function (obj) {
      this.projectObj = obj;
      this.curId = 1;
      console.log(obj)
      this.ProjectModelShow = true;
    },

    // 跳转到预览
    toSrc: function (url) {
      if(url == null || url == undefined || url == ''){
        layer.msg('暂无附件', {
          time: 3000, //3s后自动关闭
        });
        return;
      }
      window.open(urls.baseUrl + url);
    },
    toSrcThe: function (url) {
      console.log(url)
      if(url == null || url == undefined || url == ''){
        layer.msg('暂无附件', {
          time: 3000, //3s后自动关闭
        });
        return;
      }
      window.open( url);

    },

    //下载资源
    downloadResources: function(filePath) {
      if(filePath != null && filePath != "") {
        window.location.href = this.urls.download + "?path=" + filePath;
      }else {
        layer.msg('暂无附件', {
          time: 3000, //3s后自动关闭
        });
      }
    },

    //删除附件
    delAttr: function () {
      this.contract = '';
      this.FileUpload = [];
    },
    subStrThe: function (str) {
      if(str) {
        if (str.length > 10) {
          let s = str.substr(0, 8);
          return s + '...';
        } else {
          return str;
        }
      }

    },

	}
}
